package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Article;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 热点话题 Adapter
 * Created by wjy on 2018/10/12.
 */
public class HotListItemAdapter extends BaseListAdapter<Article, HotListItemAdapter.ViewHolder> {

    public HotListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hot_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Article article = mData.get(position);
        if(article!=null&&article.getInvid()!=1){
            holder.content_lay.setVisibility(View.VISIBLE);

            holder.title.setText(article.getTitle());
            try {
                holder.content.setText(article.getContent().replace(Constant.ARTICLE_TAG,"")
                        .replace(Constant.ARTICLE_P1,"")
                        .replace(Constant.ARTICLE_P2,""));
            }catch (Exception e){
                e.printStackTrace();
                holder.content.setText("");
            }
            String images = article.getImgurls();
            if(!TextUtils.isEmpty(images)){
                images = images.split(",")[0];
                Glide.with(mContext).load(Constant.IMAGE_PREFIX + images).into(holder.image);
            }
            holder.views.setText(String.valueOf(article.getView()));

        }else {
            holder.content_lay.setVisibility(View.INVISIBLE);
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title, content, views;
        ImageView image;
        RelativeLayout content_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            views = itemView.findViewById(R.id.tv_views);
            content = itemView.findViewById(R.id.tv_content);
            image = itemView.findViewById(R.id.iv_image);
            content_lay = itemView.findViewById(R.id.content_lay);

        }
    }


}
